package com.xiangwei.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;













import com.xiangwei.dao.GodMapper;
import com.xiangwei.dao.ProductMapper;
import com.xiangwei.dao.QrcodeMapper;
import com.xiangwei.dao.ScanRecordMapper;
import com.xiangwei.dao.SummaryMapper;
import com.xiangwei.entity.Product;
import com.xiangwei.entity.Summary;
import com.xiangwei.service.ISummaryService;
import com.xiangwei.util.BarCharts;
import com.xiangwei.util.Common;
import com.xiangwei.util.DateUtils;
import com.xiangwei.view.EchartsView;
import com.xiangwei.view.SeriesView;

@Service
public class SummaryServiceImpl implements ISummaryService{

	@Resource
	private GodMapper godMapper;
	@Resource
	private SummaryMapper summaryMapper;
	@Resource
	private QrcodeMapper qrcodeMapper;
	@Resource
	private ProductMapper productMapper;
	@Resource
	private ScanRecordMapper scanrecordMapper;
	
	@Override
	public int insertSelective(Summary record) {
		return summaryMapper.insertSelective(record);
	}
	@Override
	public int updateSelective(Summary record) {
		return summaryMapper.updateByPrimaryKey(record);
	}
	@Override
	public Summary selectSummary() {
		return summaryMapper.selectSummary();
	}
//
//	@Override
//	public PortalSummary selectByCustomerId(int customerId) {
//		return portalSummaryMapper.selectByCustomerId(customerId);
//	}
	
	
	@Override
	public EchartsView selectECharts(Map<String, Object> map) {
		int days = (int) map.get("days");
		int day = -days;
		Map<String, Object> paraMap = new HashMap<String, Object>();
		Date endDate = DateUtils.getCurrentDate();
		paraMap.put("endDate", endDate);
		paraMap.put("startDate", DateUtils.addDay(endDate, day));
		
		Map<String, Object> dateMap = dateRulsSet(paraMap);
		int size = (int) dateMap.get("size");
		Date startDate = (Date) dateMap.get("startDate");
		int rulsDay = (int) dateMap.get("rulsDay");
		//图表对象一
		SeriesView svScandCount = new SeriesView();
		svScandCount.setName("扫码总数");
		svScandCount.setType("line");
		Object[] scanArray = new Object[size];
		
		//横轴数据：显示搜索的时间段
		String[] xData = new String[size];
		
		List<Product> list = productMapper.getAllProduct();

		for(int i = 0 ; i<size ; i++){	
			int count = 0;
			if(i != 0){
				startDate = DateUtils.addDay(startDate, rulsDay);
			}
			xData[i] = DateUtils.dateToString(startDate, DateUtils.PATTEN_YMD);
			//从DB中取得图表数据, 扫码总数
			map.put("startDate", startDate);
			map.put("endDate", DateUtils.addDay(startDate, rulsDay));						
			if(Common.isNullOrEmpty(list) || list.size() == 0){//获取全部产品
				scanArray[i] = 0;
			}else{
				for(Product p : list){//循环读取每个产品的扫码数量
					String tableName = "qlm_portal_scanrecord_"+p.getId();					 
					if(qrcodeMapper.existTable(tableName) >0){//如果该产品扫码表存在，统计扫码次数
						HashMap<String, Object> map1 = new HashMap<String, Object>();
						map1.put("productId",p.getId());
						map1.put("startDate", startDate);
						map1.put("endDate", DateUtils.addDay(startDate, rulsDay));
						count += scanrecordMapper.findScanrecordCountInDay(map1);
					}
				}
				scanArray[i] = count;
			}							
		}
		svScandCount.setData(scanArray);		
			
		// 设置主标题与副标题
		Map<String, String> title = new HashMap<String, String>();
		title.put("text", "二维码扫码概况");
		title.put("subtext", "扫码总数");
		// 设置图例
		Map<String, String[]> legend = new HashMap<String, String[]>();
		String[] date = {"扫码总数" };
		legend.put("data", date);
		// 直角坐标系中的横轴数据
		Map<String, Object> xAxis = new HashMap<String, Object>();
		xAxis.put("data", xData);
		// 图表数据
		List<SeriesView> svList = new ArrayList<SeriesView>();
		svList.add(svScandCount);
		//获得图表数据
		EchartsView ehv = BarCharts.barOptionCombined(title, legend, xAxis,svList);
		return ehv;
	}
	
	@Override
	public EchartsView qrcodeECharts(Map<String, Object> map) {
		Map<String, Object> paraMap = new HashMap<String, Object>();
		//获得查询的时间段相关参数
		paraMap = dateRulsSet(map);
		int size = (int) paraMap.get("size");
		Date startDate = (Date) paraMap.get("startDate");
		int rulsDay = (int) paraMap.get("rulsDay"); 
		//图表对象一
		SeriesView svScandCount = new SeriesView();
		svScandCount.setName("扫码总数");
		svScandCount.setType("line");
		Object[] scanArray = new Object[size];
		
		//横轴数据：显示搜索的时间段
		String[] xData = new String[size];
		String id = (String) map.get("productId");
		String tableName = "qlm_portal_scanrecord_"+id;
		int count = qrcodeMapper.existTable(tableName); 
		for(int i = 0 ; i<size ; i++){
				
			if(i != 0){
				startDate = DateUtils.addDay(startDate, rulsDay);
			}
			xData[i] = DateUtils.dateToString(startDate, DateUtils.PATTEN_YMD);
			//从DB中取得图表数据, 扫码总数
			map.put("startDate", startDate);
			map.put("endDate", DateUtils.addDay(startDate, rulsDay));						
			if(count == 0 ){//该产品未产码
				scanArray[i] = 0;
			}else{
				scanArray[i] = scanrecordMapper.findScanrecordCountInDay(map);//每天扫码总数				
			}
		}
		svScandCount.setData(scanArray);
		// 设置主标题与副标题
		Map<String, String> title = new HashMap<String, String>();
		title.put("text", "二维码扫码概况");
		title.put("subtext", "扫码总数");
		// 设置图例
		Map<String, String[]> legend = new HashMap<String, String[]>();
		String[] date = {"扫码总数" };
		legend.put("data", date);
		// 直角坐标系中的横轴数据
		Map<String, Object> xAxis = new HashMap<String, Object>();
		xAxis.put("data", xData);
		// 图表数据
		List<SeriesView> svList = new ArrayList<SeriesView>();
		svList.add(svScandCount);
		//获得图表数据
		EchartsView ehv = BarCharts.barOptionCombined(title, legend, xAxis,svList);
		return ehv;
	}
	
	@Override
	public EchartsView godECharts(Map<String, Object> map) {
		Map<String, Object> dateMap = new HashMap<String, Object>();
		//获得查询的时间段相关参数
		dateMap = dateRulsSet(map);
		int size = (int) dateMap.get("size");
		Date startDate = (Date) dateMap.get("startDate");
		int rulsDay = (int) dateMap.get("rulsDay"); 
		//图表对象一：新增用户数据
		SeriesView svTotalCount = new SeriesView();
		svTotalCount.setName("新增用户");
		svTotalCount.setType("line");
		Object[] totalArray = new Object[size];
		
		//横轴数据：显示搜索的时间段
		String[] xData = new String[size];
		String id = (String) map.get("productId");
		String tableName = "qlm_portal_god_"+id;
		int count = qrcodeMapper.existTable(tableName);
		
		for(int i = 0 ; i<size ; i++){
				
			if(i != 0){
				startDate = DateUtils.addDay(startDate, rulsDay);
			}
			xData[i] = DateUtils.dateToString(startDate, DateUtils.PATTEN_YMD);
			//从DB中取得图表数据, 每天新增用户人数
			map.put("startDate", startDate);
			map.put("endDate", DateUtils.addDay(startDate, rulsDay));			 			
			if(count == 0 ){//该产品未产码
				totalArray[i] = 0;
			}else{
				totalArray[i] = godMapper.findGodCountInDay(map);//每天增长用户数量
			}
			
		}
		svTotalCount.setData(totalArray);
		// 设置主标题与副标题
		Map<String, String> title = new HashMap<String, String>();
		title.put("text", "每日新增用户概况");
		title.put("subtext", "新增用户统计");
		// 设置图例
		Map<String, String[]> legend = new HashMap<String, String[]>();
		String[] date = { "新增用户" };
		legend.put("data", date);
		// 直角坐标系中的横轴数据
		Map<String, Object> xAxis = new HashMap<String, Object>();
		xAxis.put("data", xData);
		// 图表数据
		List<SeriesView> svList = new ArrayList<SeriesView>();
		svList.add(svTotalCount);
		//获得图表数据
		EchartsView ehv = BarCharts.barOptionCombined(title, legend, xAxis,svList);
		return ehv;
	}

	/**
	 * 获得查询的时间参数
	 * @param map
	 * @return
	 */
	private Map<String, Object> dateRulsSet(Map<String, Object> map){
		Date startDate = null;//开始时间
		Date endDate = null;//结束时间
		int betweenDate = 0;//相差天数
		int rulsDay = 1;//横轴显示的单位
		int size = 1;//数组长度
		if(map.get("startDate") != null){
			startDate = (Date) map.get("startDate");
		}
		if(map.get("endDate") != null){
			endDate = (Date) map.get("endDate");
		}
		if(startDate != null && endDate !=null){
			betweenDate = DateUtils.getDiffDay(startDate, endDate) + 1;
		}
		//横轴显示的规则:如果时间段小于7天则以1天为单位，如果小于15天则以2天为单位...
		if(betweenDate > 7 && betweenDate < 15){
			rulsDay = 1;
		}else if(betweenDate >=15 && betweenDate < 30){
			rulsDay = 1;
		}else if(betweenDate >=30 && betweenDate < 60){
			rulsDay = 1;
		}
		if(betweenDate != 0){
			size = (int) Math.ceil((double)betweenDate/rulsDay);
		}
		//返回参数的设定
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("startDate",startDate);
		returnMap.put("rulsDay",rulsDay);
		returnMap.put("size",size);
		return returnMap;
	}

	
}
